'use strict';

const Controller = require('egg').Controller;
const nm = "form";
class contrl extends Controller {
    async save(){
        const {ctx,app,service} = this;
        let param = ctx.request.body;
        if(!param.M){
            ctx.error(app.resCode.params);
        }else{
            try{
                param.M.uid = ctx.session.userinfo._id;
                let res = await service.form.save(param);
                ctx.success(res);
            }catch(e){
                ctx.error(app.resCode.params,e.message || e);
            }
        }
    }
    async remove(){
        const {ctx,app,service} = this;
        let id = ctx.request.body.id || "";
        if(!id){
         ctx.error(app.resCode.params);
        }else{
             try{
                 let res = await service[nm].remove(id);
                 ctx.success(res);
             }catch(e){
                 ctx.error(app.resCode.params,e.message || e);
             }
        }
    }
   async list(){
    const {ctx,app,service} = this;
    let query = ctx.query;
    let uid = ctx.session.userinfo._id;
    try{
        let res = await service[nm].list(query,uid);
        ctx.success(res);
    }catch(e){
        ctx.error(app.resCode.params,e.message || e);
    }
   }
   async info(){
    const {ctx,app,service} = this;
    let id = ctx.query.id;
    if(!id){
        ctx.error(app.resCode.params);
    }else{
        try{
            let res = await service[nm].info(id);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        } 
    }
   }
   async submit(){
    const {ctx,app,service} = this;
        try{
            let res = await service[nm].submit(ctx.request.body);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        } 
   }
   async removeData(){
    const {ctx,app,service} = this;
        let id = ctx.request.body.id || "";
        if(!id){
         ctx.error(app.resCode.params);
        }else{
             try{
                 let res = await service[nm].removeData(id);
                 ctx.success(res);
             }catch(e){
                 ctx.error(app.resCode.params,e.message || e);
             }
        }
   }
   async infoData(){
    const {ctx,app,service} = this;
        let id = ctx.query.id || "";
        if(!id){
         ctx.error(app.resCode.params);
        }else{
             try{
                 let res = await service[nm].infoData(id);
                 ctx.success(res);
             }catch(e){
                 ctx.error(app.resCode.params,e.message || e);
             }
        }
   }
   async listData(){
    const {ctx,app,service} = this;
    let query = ctx.query;
    if(!query.FORMID){
        ctx.error(app.resCode.params);
    }else{
        try{
            let res = await service[nm].listData(query);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        }
    }
   }
}

module.exports = contrl;